package com.lrkj.property.service.impl;

import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.property.domain.AdvertManag;
import com.lrkj.property.domain.AdvertPostManag;
import com.lrkj.property.mapper.AdvertPostManagMapper;
import com.lrkj.property.service.IAdvertManagService;
import com.lrkj.property.service.IAdvertPostManagService;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 广告位管理Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-03-16
 */
@Service
public class AdvertPostManagServiceImpl implements IAdvertPostManagService {
    @Autowired
    private AdvertPostManagMapper advertPostManagMapper;

    @Autowired
    private IOrgTreeService orgTreeService;

    @Autowired
    private IAdvertManagService advertManagService;

    /**
     * 查询广告位管理
     *
     * @param id 广告位管理ID
     * @return 广告位管理
     */
    @Override
    public AdvertPostManag selectAdvertPostManagById(String id) {
        return advertPostManagMapper.selectAdvertPostManagById(id);
    }

    /**
     * 查询广告位管理列表
     *
     * @param advertPostManag 广告位管理
     * @return 广告位管理
     */
    @Override
    public List<AdvertPostManag> selectAdvertPostManagList(AdvertPostManag advertPostManag) {
        return advertPostManagMapper.selectAdvertPostManagList(advertPostManag);
    }

    /**
     * 新增广告位管理
     *
     * @param advertPostManag 广告位管理
     * @return 结果
     */
    @Override
    public int insertAdvertPostManag(AdvertPostManag advertPostManag) {
        advertPostManag.setId(IdUtils.simpleUUID());
        advertPostManag.setCreateBy(SecurityUtils.getUsername());
        advertPostManag.setCreateTime(DateUtils.getNowDate());
        this.setUnitInfo(advertPostManag);
        return advertPostManagMapper.insertAdvertPostManag(advertPostManag);
    }

    /**
     * 修改广告位管理
     *
     * @param advertPostManag 广告位管理
     * @return 结果
     */
    @Override
    public int updateAdvertPostManag(AdvertPostManag advertPostManag) {
        advertPostManag.setUpdateTime(DateUtils.getNowDate());
        advertPostManag.setUpdateBy(SecurityUtils.getUsername());
        this.setUnitInfo(advertPostManag);
        return advertPostManagMapper.updateAdvertPostManag(advertPostManag);
    }


    private void setUnitInfo(AdvertPostManag bean) {
        AdvertManag parentBean = advertManagService.selectAdvertManagById(bean.getAdvertId());
        bean.setAncestors(parentBean.getAncestors() + "," + bean.getAdvertId());
        bean.setDeptId(parentBean.getDeptId());
        orgTreeService.insertOrgTree(bean.getId(), bean.getAdvertPostName(), bean.getAdvertId(), OrgConstants.advert_post, parentBean.getAncestors(), bean.getOrderNum());
    }

    /**
     * 批量删除广告位管理
     *
     * @param ids 需要删除的广告位管理ID
     * @return 结果
     */
    @Override
    public int deleteAdvertPostManagByIds(String[] ids) {
        orgTreeService.deleteOrgTreeByIds(ids);
        return advertPostManagMapper.deleteAdvertPostManagByIds(ids);
    }

    /**
     * 删除广告位管理信息
     *
     * @param id 广告位管理ID
     * @return 结果
     */
    @Override
    public int deleteAdvertPostManagById(String id) {
        return advertPostManagMapper.deleteAdvertPostManagById(id);
    }

    /**
     * 批量新增广告位管理信息
     *
     * @param advertPostManags 广告位管理对象
     */
    public int batchAddData(List<AdvertPostManag> advertPostManags) {
        return advertPostManagMapper.batchAddData(advertPostManags);
    }
}
